
#### APPENDIX C7: executive opposition


# load, combine data
library(rio)
library(tidyverse)

# estimate models
library(brglm2)

# cluster standard errors
library(lmtest)
library(sandwich)

# generate tables
library(stargazer)




# LOAD DATA
glm_df <- import("glm_data.csv") %>%
  filter(iso3c!="TTO" & iso3c!="GUY" & iso3c!="SUR") %>%
  filter(!is.na(approval) & !is.na(opp1vote) & !is.na(number_protests))


## TABLE C11
brglm_exec1 <- glm(doc ~ percent_opp_exec +
                     as.factor(iso3c) + as.factor(quarter) + as.factor(year),  
              family = binomial(link="logit"), 
              method = "brglmFit",
              data = glm_df)

brglm_exec1_r <- brglm_exec1 %>%
  coeftest(vcovHC(brglm_exec1, type = 'HC0', cluster =  ~ iso3c))



brglm_exec2 <- glm(doc ~ approval + percent_opp_exec + number_protests + previous_doc + duck + partyage + polity2 + left_exec + 
                   any_election + minister_is_technocrat_mainstream + 
                   resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
                   as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
                   family = binomial(link="logit"), 
                   method = "brglmFit",
                   data = glm_df)

brglm_exec2_r <- brglm_exec2 %>%
  coeftest(vcovHC(brglm_exec2, type = 'HC0', cluster =  ~ iso3c))



brglm_exec3 <- glm(fund_doc ~ percent_opp_exec +
                   as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
                   family = binomial(link="logit"), 
                   method = "brglmFit",
                   data = glm_df)

brglm_exec3_r <- brglm_exec3 %>%
  coeftest(vcovHC(brglm_exec3, type = 'HC0', cluster =  ~ iso3c))



brglm_exec4 <- glm(fund_doc ~ approval + percent_opp_exec + number_protests + previous_fund_doc + duck + partyage + polity2 + left_exec + 
                   any_election + minister_is_technocrat_mainstream + 
                   resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
                   as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
                   family = binomial(link="logit"), 
                   method = "brglmFit",
                   data = glm_df)

brglm_exec4_r <- brglm_exec4 %>%
  coeftest(vcovHC(brglm_exec4, type = 'HC0', cluster =  ~ iso3c))



# generate tables
stargazer(brglm_exec1_r,brglm_exec2_r,brglm_exec3_r,brglm_exec4_r, #type="text",
          omit=c("iso3c","year","quarter"), no.space = T,
          covariate.labels = c("Executive approval (perc)","Executive opposition vote share (perc)","Number of protests",
                               "Previous document = 1","Previous fund document = 1","Lame duck = 1", "Party age",
                               "Democracy","Left executive = 1",
                               "Election quarter = 1","Mainstream minister = 1",
                               "Resource rents (perc)","Field discovery = 1","Oil price (USD, log)",
                               "GDP per capita (1,000 USD)","GDP growth (perc)","IMF program = 1", "Crisis = 1"), 
          dep.var.labels = c("Any document","Fund document"))

# retrieve n, aic, ll, etc
stargazer(brglm_exec1,brglm_exec2,brglm_exec3,brglm_exec4, #type="text",
          omit=c("iso3c","year","quarter"), no.space = T,
          dep.var.labels = c("Any document","Fund document"))




